<?php 

Class starModel extends appModel
{
	
	
	
	//获取星星数
	public function getInfo($wxid){
		$sql="SELECT nickname,hits FROM ".$this->table('wxusers')." WHERE id= '".$wxid."'";
		
		return $res=$this->getRow($sql);
	}
	
	
	public function is_user_existed($openid){
		$sql="SELECT id FROM ".$this->table('wxusers')." WHERE openid= '".$openid."'";
		$res=$this->getOne($sql);
		
		return $res;
	}
	public function updatemsg($openid,$nickname,$sex,$city,$country,$province){
		$sql="UPDATE ".$this->table('wxusers')." SET nickname='".$nickname."' , sex='".$sex."' , city='".$city."' , country='".$country."' , province='".$province."' , is_hits=1 , hits =0 WHERE openid = '".$openid."'";
		$res=$this->query($sql);
		return $this->insert_id();
	}
	
	

	public function updateUser($info,$wxid){
	
		$sql="UPDATE ".$this->table('wxusers')." SET headimgurl = '".$info."' WHERE id =".$wxid;
		$res=$this->query($sql);
	
		return true;
	}
	
	public function insertUser($info){
		
		$field='';
		$value='';
		$result=array('error'=>0,'content'=>'');

			foreach($info as $k=>$v)
			{
				if($k!='privilege')
				{
					$field.="$k,";
						
						
					$value.="'$v',";
			
				}
			}
		
			$field=substr($field,0,strlen($field)-1);
			$value=substr($value,0,strlen($value)-1);
			
			$sql="INSERT INTO ".$this->table('wxusers')." (".$field.") VALUES(".$value.")";
			if(!$res=$this->query($sql))
			{
				$result['error']=1;
				$result['content']=Lang('SQL_ERROR');
			}
		
	
		
		return $this->insert_id();
	}
	public function checknick($openid,$nickname){
		$sql="SELECT * FROM ".$this->table('wxusers')." WHERE openid= '".$openid."' and nickname='".$nickname."'";
		return $res=$this->getAll($sql);
	}
	public function checknick2($openid){
		$sql="SELECT * FROM ".$this->table('wxusers')." WHERE openid= '".$openid."'";
		$res=$this->getAll($sql);
		$arr = array();
		foreach ($res AS $idx => $row)
		{
			$arr[$idx]['nickname']          = $row['nickname'];
		}
		if($arr){
			return 1;
		}else{
			return 0;
		}
	}
	public function checkid2($openid){
		$sql="SELECT * FROM ".$this->table('wxcheck')." WHERE wxid= '".$openid."'";
		return $res=$this->getAll($sql);
	}
	public function is_appid()
    {
    	$sql="SELECT op_value FROM ".$this->table('options')." WHERE op_key='appid'";
    	$is_appid=$this->getOne($sql);
		
    	return  $is_appid;
    }
	public function is_ser()
    {
    	$sql="SELECT op_value FROM ".$this->table('options')." WHERE op_key='appsecret'";
    	$is_ser=$this->getOne($sql);
    	
    	return  $is_ser;
    }
	public function is_ass_time()
    {
    	$sql="SELECT op_value FROM ".$this->table('options')." WHERE op_key='ass_time'";
    	$is_time=$this->getOne($sql);
    	
    	return  $is_time;
    }
	public function update_time($time){
		$sql="UPDATE ".$this->table('options')." SET op_value = '".$time."' where op_key='ass_time'";
		return $res=$this->query($sql);
	}
	public function update_token($token){
		$sql="UPDATE ".$this->table('options')." SET op_value = '".$token."' where op_key='ass_token'";
		return $res=$this->query($sql);
	}
	
	public function getToken()
    {
    	$sql="SELECT op_value FROM ".$this->table('options')." WHERE op_key='ass_token'";
    	$is_token=$this->getOne($sql);
    	
    	return  $is_token;
    }
}
	